﻿using DotnetSpider.Core;
using DotnetSpider.Core.Processor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.DotNetSpiderSample.Models;

namespace Zj.DotNetSpiderSample
{
    public class CNBlogSpider
        : BasePageProcessor
    {
        protected override void Handle(Page page)
        {
            List<CNBlogEntity> blogs = new List<CNBlogEntity>();
            var blogNodes = page.Selectable.XPath("//*[@class='post_item']").Nodes();
            foreach (var blogNode in blogNodes)
            {
                var title = blogNode.XPath(".//a[@class='titlelnk']")
                    .GetValue(DotnetSpider.Core.Selector.ValueOption.InnerText)
                    .Trim();
                var content = blogNode.XPath(".//p[@class='post_item_summary']")
                    .GetValue(DotnetSpider.Core.Selector.ValueOption.InnerText)
                    .Trim();
                var remark = blogNode.XPath(".//div[@class='post_item_foot']")
                    .GetValue(DotnetSpider.Core.Selector.ValueOption.InnerText)
                    .Trim();
                var blog = new CNBlogEntity()
                {
                    Content = content,
                    Title = title,
                    Remark = remark
                };
                blogs.Add(blog);
            }
            page.AddResultItem("BlogList", blogs);
            var pageNodes = page.Selectable.XPath(".//div[@class='pager']/a").Nodes();
            var pageNode = pageNodes.ElementAt(pageNodes.Count() - 2);
            var pageCount = Convert.ToInt32(pageNode.GetValue(DotnetSpider.Core.Selector.ValueOption.InnerText).Trim());
            var baseUrl= "https://www.cnblogs.com/#p";
            for (int i = 0; i < pageCount; i++)
            {
                var url = $"{baseUrl}{i+1}";
                page.AddTargetRequest(url);
            }
        }
    }
}
